package view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import main.Log;

/**
 * @author polyphemus
 * Implements the definition of a GameButton 
 */
public class GameButton  
{
	private List<MouseListener> listeners;
	private Rectangle rect;
	private String name;
	private Image image;
	
	/**
	 * This creates a GameButton with the specified rectangle.  Rectangle should be a unique position on the GameWindow
	 * for the user to interact with and its MouseListener should be attached after its instantiation. 
	 * @param name The name of the GameButton 
	 * @param rect A rectangle with the x, y, and size of the rectangle on GameWindow
	 */
	public GameButton(String name, Rectangle rect, Image bufferedImage) {
		listeners = new ArrayList<MouseListener>();
		this.name = name;
		this.setRect(rect);
		this.setImage(bufferedImage);
	}
	/**Activates the mouseClicked event for this GameButton
	 * @param e The MouseEvent that occurred within this GameButton 
	 */
	public void activateMouseClicked(MouseEvent e) {
		for( int i = 0 ; i < this.listeners.size() ; i++)  {
			listeners.get(i).mouseClicked(e);
		}
//		for(MouseListener ml : listeners) {
//			ml.mouseClicked(e);
//		}			
	}
	/**Activates the mouseEntered event for this GameButton
	 * @param e The MouseEvent that occurred within this GameButton 
	 */
	public void activateMouseEntered(MouseEvent e) {
		for( int i = 0 ; i < this.listeners.size() ; i++)  {
			listeners.get(i).mouseEntered(e);
		}
//		for(MouseListener ml : listeners) {
//			ml.mouseEntered(e);
//		}
	}
	/**Activates the mouseExited event for this GameButton
	 * @param e The MouseEvent that occurred within this GameButton 
	 */
	public void activateMouseExited(MouseEvent e) {
		for( int i = 0 ; i < this.listeners.size() ; i++)  {
			listeners.get(i).mouseExited(e);
		}
//		for(MouseListener ml : listeners) {
//			ml.mouseExited(e);
//		}
	}
	/**Activates the mousePressed event for this GameButton
	 * @param e The MouseEvent that occurred within this GameButton 
	 */
	public void activateMousePressed(MouseEvent e) {
		for( int i = 0 ; i < this.listeners.size() ; i++)  {
			listeners.get(i).mousePressed(e);
		}
//		for(MouseListener ml : listeners) {
//			ml.mousePressed(e);
//		}
	}
	/**Activates the mouseReleased event for this GameButton
	 * @param e The MouseEvent that occurred within this GameButton 
	 */
	public void activateMouseReleased(MouseEvent e) {
		for( int i = 0 ; i < this.listeners.size() ; i++)  {
			listeners.get(i).mouseReleased(e);
		}
//		for(MouseListener ml : listeners) {
//			ml.mouseReleased(e);
//		}
	}
	/**The draw method
	 * @param g The Graphics one must pass so that this Button can be drawn.
	 */
	public void draw(Graphics g) {
		g.drawRect( rect.x, rect.y, rect.width, rect.height);
		if( image != null ) {
			g.drawImage( image, rect.x, rect.y, rect.x + rect.width, rect.y + rect.height,
					0, 0, image.getWidth(null), image.getHeight(null), null );
		}
	}
	/**The method where one can register a MouseListener to this GameButton
	 * @param l The MouseListener one can register to this GameButton
	 */
	public void attachListener(MouseListener l) {
		listeners.add(l);
	}
	/**The method where one can unregister a MouseListener to this GameButton
	 * @param l The MouseListener one can unregister to this GameButton
	 */
	public void detachListener(MouseListener l) {
		if(listeners.remove(l))
			Log.out(Log.VIEW, "The listener for this GameButton: " + this.name + "is not registered");
	}
	/**
	 * @param rect the rect to set
	 */
	private void setRect(Rectangle rect) {
		this.rect = rect;
	}
	/**
	 * @return the rect associated with this GameButton
	 */
	public Rectangle getRect() {
		return rect;
	}
	/**
	 * @param bufferedImage the bufferedImage to set
	 */
	public void setImage(Image image) {
		this.image = image;
	}
	/**
	 * @return the bufferedImage
	 */
	public Image getImage() {
		return image;
	}
}
